// 用户模块
const state = {
  profile: null,
  isLoggedIn: false
}

const getters = {
  userProfile: state => state.profile,
  isLoggedIn: state => state.isLoggedIn
}

const mutations = {
  SET_PROFILE(state, profile) {
    state.profile = profile
  },
  SET_LOGIN_STATUS(state, status) {
    state.isLoggedIn = status
  }
}

const actions = {
  login({ commit }, credentials) {
    // 模拟登录
    return new Promise((resolve) => {
      setTimeout(() => {
        const profile = {
          id: 1,
          username: credentials.username,
          email: `${credentials.username}@example.com`
        }
        commit('SET_PROFILE', profile)
        commit('SET_LOGIN_STATUS', true)
        resolve(profile)
      }, 1000)
    })
  },
  
  logout({ commit }) {
    commit('SET_PROFILE', null)
    commit('SET_LOGIN_STATUS', false)
  }
}

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}